<?php
include("system/system.php");
session_start();
$act=daddslashes($_GET["act"]);
if(isset($_COOKIE["user"])){
    $token=authcode($_COOKIE["user"],'DECODE','codecj80808');
    $k=explode("##",$token);
    $query=$pdo->prepare("SELECT * FROM 9g_user WHERE `username`=:username and `password`=:password ");
    $query->bindParam(":username",$k[0],PDO::PARAM_STR);
    $query->bindParam(":password",$k[1],PDO::PARAM_STR);
    $query->execute();
    if($query->rowCount()>=1){
        $login=1;
    }
}else{
    $login=0;
}
switch ($act){
   case 'checkuser':
       function data_act($data){
        $s=explode("||",$data);
        foreach($s as $k=>$v){
        $x.=$v.'<hr>';
        }
        return $x;
       }
        function type_if($id){
            global $pdo;
            return $pdo->query("SELECT * FROM 9g_type WHERE `id`='{$id}'")->fetch()["type"];
        }
        if(isset($_POST["data"])){
            $data=daddslashes($_POST["data"]);
            $query=$pdo->prepare("SELECT * FROM 9g_msg WHERE data REGEXP :data and `status`='1'");
            $query->bindParam(":data",$data,PDO::PARAM_INT,50);
            $query->execute();
            $count=$query->rowCount();
            if($count==1){
                $state=1;
                $row=$query->fetch(PDO::FETCH_ASSOC);
                $im=explode(",",$row["image"]);
                foreach($im as $k=>$v){
                    $image.='  <a class="float-start" href="'.$v.'" title="证据截图">
                    <div class="img-fluid"><img src="'.$v.'" alt="img-1" width="120"></div>
                </a>';
                }
               $msg='    <div class="col-md-8 offset-md-2">
               <div class="card border border-success">
                   <div class="card-header bg-transparent border-success">
                       <h5 class="my-0 text-success"><i class="uil uil-check-circle me-3"></i>查询到1记录</h5>
                   </div>
                   <div class="card-body">
                       <div class="faq-box d-flex mt-4">
                           <div class="flex-shrink-0 me-3">
                               <div class="avatar-xs"><img src="assets/image/zp.png" width="30px" height="30px;" alt=""></div>
                           </div>
                           <div class="flex-grow-1">
                               <h5 class="font-size-15 ">收录信息：</h5>
                               <a href="#" style="margin-left:90%;" class="badge bg-info">'.type_if($row["type"]).'</a><p style="color: red;">'.data_act($row["data"]).'</p>
                           </div>
                       </div>
                       <hr>
                       <div class="faq-box d-flex mt-4">
                           <div class="flex-shrink-0 me-3">
                               <div class="avatar-xs"><img src="assets/image/ly.png" width="30px" height="30px;" alt=""></div>
                           </div>
                           <div class="flex-grow-1">
                               <h5 class="font-size-15">理由及证据：</h5>
                               <p class="text-muted">'.$row["msg"].'</p>
                           </div>
                       </div>
                       <hr>
                       <div class="faq-box d-flex mt-4">
                           <div class="flex-shrink-0 me-3">
                               <div class="avatar-xs"><img src="assets/image/zj.png" width="30px" height="30px;" alt=""></div>
                           </div>
                           <div class="flex-grow-1">
                               <h5 class="font-size-15">证据截图：</h5>
                               <div class="popup-gallery">
                             '.$image.'
                           </div>
                           </div>
                       </div>
                       <hr>
                       <div class="faq-box d-flex mt-4">
                           <div class="flex-shrink-0 me-3">
                               <div class="avatar-xs"><img src="assets/image/kt.png" width="30px" height="30px;" alt=""></div>
                           </div>
                           <div class="flex-grow-1">
                               <h5 class="font-size-15"><button onclick="return zsy('.$row["id"].');" class="btn btn-outline-success" style="font-size: 10px;">生成专属页</a></button>

                           </div>
                       </div>
                   </div>
               </div>
           </div>    <script src="assets/js/jquery.magnific-popup.min.js"></script>
           <script src="assets/js/lightbox.init.js"></script>';
            }else if($count>1){
                       $msg='    <div class="col-md-8 offset-md-2">
               <div class="card border border-success">
                   <div class="card-header bg-transparent border-success">
                       <h5 class="my-0 text-success"><i class="uil uil-check-circle me-3"></i>查询到'.$count.'记录</h5>
                   </div>
                   <div class="card-body"><div class="row">';
                while($row=$query->fetch(PDO::FETCH_ASSOC)){
                    $msg.='<div class="col-lg-6">
                                <div class="card bg-dark text-light">
                                    <div class="card-body">
                                        <h5 class="mb-4 text-light"><i class="uil uil-arrow-circle-right me-3"></i>'.type_if($row["type"]).':'.$row["data"].'<button onclick="return zsy('.$row["id"].')" type="button" class="btn btn-outline-info waves-effect waves-light">生成专属页</button></h5>
                                        
                                        </div></div></div>';
                }
                       $msg.=' </div>   </div>
               </div>
           </div>';
                $state=1;
            }else{
                $state=0;
            }
            die(json_encode(["state"=>$state,"msg"=>$msg]));
        }
        break;
    case 'addyou':
        if(isset($_POST["name"]) && isset($_POST["url"]) && isset($_POST["favicon"])){
            $name=daddslashes($_POST["name"]);
            $url=daddslashes($_POST["url"]);
            $favicon=daddslashes($_POST["favicon"]);
            $ip=real_ip();
            $date=date('Y-m-d h:i:s', time());
            $datas=$pdo->query("SELECT * FROM 9g_you WHERE `ip`='{$ip}'")->fetchAll();
            $count=count($datas);
                if($count>=$home["ip"]){
                if(abs(strtotime($datas[$count-1]["time"])-strtotime($date)) > 24*3600){
            $query=$pdo->prepare("INSERT INTO 9g_you(`name`,`url`,`favicon`,`status`,`ip`,`time`) VALUES(:name,:url,:favicon,'0','{$ip}','{$date}')");
            $query->bindParam(":name",$name,PDO::PARAM_STR);
            $query->bindParam(":url",$url,PDO::PARAM_STR);
            $query->bindParam(":favicon",$favicon,PDO::PARAM_STR);
            $query->execute();
            $msg=$query->fetch();
            $state=1;
                }else{
                $state=-1;
                }
                }else{
            $query=$pdo->prepare("INSERT INTO 9g_you(`name`,`url`,`favicon`,`status`,`ip`,`time`) VALUES(:name,:url,:favicon,'0','{$ip}','{$date}')");
            $query->bindParam(":name",$name,PDO::PARAM_STR);
            $query->bindParam(":url",$url,PDO::PARAM_STR);
            $query->bindParam(":favicon",$favicon,PDO::PARAM_STR);
            $query->execute();
            $msg=$query->fetch(); 
            $state=1;
                }
            die(json_encode(["state"=>$state]));
        }
        break;
    case 'addinfo':
        if (isset($_POST["type"]) && isset($_POST["data"]) && isset($_POST["image"]) && isset($_POST["user"]) && isset($_POST["msg"])){
            $type=daddslashes($_POST["type"]);
            $data=strip_tags(addslashes(daddslashes($_POST["data"])));
            $image=daddslashes($_POST["image"]);
            $user=daddslashes($_POST["user"]);
            $msg=strip_tags(addslashes(daddslashes($_POST["msg"])));
            $date=date('Y-m-d h:i:s', time());
            $ip=real_ip();
            if (!filter_var($user, FILTER_VALIDATE_EMAIL)) {
                $state=-2;
            } else if($login==1) {
                $datas=$pdo->query("SELECT * FROM 9g_msg WHERE `ip`='{$ip}'")->fetchAll();
                $count=count($datas);
                if($count>=$home["ip"]){
                if(abs(strtotime($datas[$count-1]["time"])-strtotime($date)) > 60){
                    if($pdo->exec("INSERT INTO 9g_msg(`data`,`msg`,`image`,`status`,`ip`,`user`,`type`,`time`) VALUES('{$data}','{$msg}','{$image}','1','{$ip}','{$user}','{$type}','{$date}')")>=1){
                        $state=2;
                    }
                    }else{
                        $state=0;
                    }
                    }else{
                        if($pdo->exec("INSERT INTO 9g_msg(`data`,`msg`,`image`,`status`,`ip`,`user`,`type`,`time`) VALUES('{$data}','{$msg}','{$image}','1','{$ip}','{$user}','{$type}','{$date}')")>=1){
                            $state=2;
                        } 
                    }
            } else{
            $datas=$pdo->query("SELECT * FROM 9g_msg WHERE `ip`='{$ip}'")->fetchAll();
            $count=count($datas);
            if($count>=$home["ip"]){
            if(abs(strtotime($datas[$count-1]["time"])-strtotime($date)) > 24*3600){
                if($pdo->exec("INSERT INTO 9g_msg(`data`,`msg`,`image`,`status`,`ip`,`user`,`type`,`time`) VALUES('{$data}','{$msg}','{$image}','0','{$ip}','{$user}','{$type}','{$date}')")>=1){
                    $state=1;
                }
            }else{
                $state=-1;
            }
        }else{
            if($pdo->exec("INSERT INTO 9g_msg(`data`,`msg`,`image`,`status`,`ip`,`user`,`type`,`time`) VALUES('{$data}','{$msg}','{$image}','0','{$ip}','{$user}','{$type}','{$date}')")>=1){
                $state=1;
            }  
        }
    }
    die(json_encode(["state"=>$state]));
}
        break;
    case 'login':
        if (isset($_POST["username"]) && isset($_POST["password"])) {
            $user = daddslashes($_POST["username"]);
            $pass = daddslashes($_POST["password"]);
            $query=$pdo->prepare("SELECT * FROM 9g_user WHERE `username`=:username and `password`=:password ");
            $query->bindParam(":username",$user,PDO::PARAM_STR);
            $query->bindParam(":password",$pass,PDO::PARAM_STR);
            $query->execute();
            if ($query->rowCount()>=1) {
                $state = '1';
                $token = $user . '##' . $pass;
                $jm = authcode($token, 'ENCODE', 'codecj80808');
                setcookie("user", $jm, time() + 3600 * 72);
            } else {
                $state = '0';
            }
            die(json_encode(array("state" => $state)));
        }
        break;
    case 'sczsy':
        if(isset($_POST["id"])){
            
            $id=daddslashes($_POST["id"]);
            $query=$pdo->prepare("SELECT * FROM 9g_msg WHERE `id`=:id and `status`='1'");
            $query->bindParam(":id",$id,PDO::PARAM_INT);
            $query->execute();
            $data=$query->fetch();
            function type_if($id){
                global $pdo;
                return $pdo->query("SELECT * FROM 9g_type WHERE `id`='{$id}'")->fetch()["type"];
            }
             function sl_if($id){
               if($id==1){
                   return '已提交百度收录';
               }else{
                   return '未提交收录';
               }
            }
                   function data_act($data){
        $s=explode("||",$data);
        foreach($s as $k=>$v){
        $x.=$v.'<hr>';
        }
        return $x;
       }
            $im=explode(",",$data["image"]);
            foreach($im as $k=>$v){
                $image.='  <a class="float-start" href="'.$v.'" title="证据截图">
                <div class="img-fluid"><img src="'.$v.'" alt="img-1" width="120" height="120"></div>
            </a>';
            }
            $msg='
            <!doctype html>
            <html lang="en">
            
            <head>
                <meta charset="utf-8">
                <title>'.$data["data"].' | 黑名单记录-'.$home["title"].'</title>
                <meta name="viewport" content="width=device-width, initial-scale=1.0">
                <meta name="description" content="'.$home["description"].'" />
                <meta name="keywords" content="'.$home["keywords"].'" />
                <link rel="shortcut icon" href="assets/images/favicon.ico"><!-- jvectormap -->
                <link href="../assets/css/jqvmap.min.css" rel="stylesheet"><!-- Bootstrap Css -->
                <link href="../assets/css/bootstrap.min.css" rel="stylesheet" type="text/css"><!-- Icons Css -->
                <link href="../assets/css/icons.min.css" rel="stylesheet" type="text/css"><!-- App Css-->
                <link href="../assets/css/app.min.css" rel="stylesheet" type="text/css">
                <link href="../assets/css/magnific-popup.css" rel="stylesheet" type="text/css">
            </head>
            
            <body data-topbar="light" data-layout="horizontal" data-layout-size="boxed">
                <!-- Start right Content here -->
                <!-- ============================================================== -->
                <div class="main-content">
            
                    <div class="container-fluid">
                        <!-- start page title -->
            
                        <div class="row">
            
                            <div class="col-md-8 offset-md-2">
                                <div class="card">
                                    <div class="card-body">
                                        <div class="row justify-content-center mt-3">
                                            <div class="col-lg-8">
                                                <div class="row">
            
                                                    <a href="javascript:history.back(-1)" class="btn btn-info waves-effect waves-light"><i class="ri-arrow-left-line align-middle ms-2"></i> 返回首页</a>
                                                </div>
                                            </div>
                                        </div><!-- end row -->
                                        <div class="row justify-content-center mt-5">
                                            <div class="col-lg-10">
                                                <div class="row justify-content-center">
                                                    <div class="col-lg-8">
                                 
                                   <div class="faq-box d-flex mt-4">
                                       <div class="flex-shrink-0 me-3">
                                           <div class="avatar-xs"><img src="../assets/image/zp.png" alt="" width="30px" height="30px;"></div>
                                       </div>
                                   <div class="flex-grow-1">
                               <h5  class="font-size-15 ">收录信息：</h5>
                                  <a id="sl" href="#" style="margin-left:10%;" class="badge bg-dark">正在检测是否被收录...</a>
                                  <a href="#" style="" class="badge bg-warning">'.sl_if($data["sl"]).'</a>
                               <a href="#"  class="badge bg-info">'.type_if($data["type"]).'</a><p style="color: red;">'.data_act($data["data"]).'</p>
                           </div>
                                   </div>
                                   <hr>
                                   <div class="faq-box d-flex mt-4">
                                       <div class="flex-shrink-0 me-3">
                                           <div class="avatar-xs"><img src="../assets/image/ly.png" alt="" width="30px" height="30px;"></div>
                                       </div>
                                       <div class="flex-grow-1">
                                           <h5 class="font-size-15">理由及证据：</h5>
                                           <p class="text-muted">'.$data["msg"].'</p>
                                       </div>
                                   </div>
                                   <hr>
                                   <div class="faq-box d-flex mt-4">
                                       <div class="flex-shrink-0 me-3">
                                           <div class="avatar-xs"><img src="../assets/image/zj.png" alt="" width="30px" height="30px;"></div>
                                       </div>
                                       <div class="flex-grow-1">
                                           <h5 class="font-size-15">证据截图：</h5>
                                           <div class="popup-gallery">
                                           '.$image.'
                                       </div>
                                       </div>
                                   </div>
                                   <hr>
                               </div>
                                                    </div>
                                                </div><!-- end row -->
                                            </div>
                                        </div>
                                        <div class="row justify-content-center">
                                            <div class="col-lg-5">
                                                <div class="text-center mt-5 mb-4">
                                                    <h5>被恶意举报?</h5>
                                                    <p class="text-muted mb-4">如上述信息包含你的信息，并且对上述信息有异议，请联系客服出示证明解决</p>
                                                    <a href="http://wpa.qq.com/msgrd?v=3&uin='.$home["qq"].'&site=qq&menu=yes" type="button" class="btn btn-primary mt-1 waves-effect waves-light">联系客服</a>
                                                </div>
                                            </div>
                                        </div><!-- end row -->
                                    </div>
                                </div>
                            </div>
                        </div><!-- end row -->
                    </div><!-- container-fluid -->
                </div>
            
                <!-- Right Sidebar -->
                <!-- Right bar overlay-->
                <script src="../assets/js/jquery.min.js"></script>
                <script src="../assets/js/jquery.magnific-popup.min.js"></script>
                <script src="../assets/js/lightbox.init.js"></script>
<script>
                   $(document).ready(function(){
                var url=window.location.href;
                         $.ajax({
            type: "POST",
            url: "../ajax.php?act=bdjc",
            data: {
                url:url
            },
            dataType: "json",
                  success: function(data) {
                   if (data.state == 1) {
                $("#sl").attr("class","badge bg-success");
                $("#sl").html(\'<img src="../assets/image/bd.png" alt="" width="10px" height="10px;">百度已收录\');
                } else {

                      $("#sl").attr("class","badge bg-warning");
                $("#sl").html(\'<img src="../assets/image/bds.png" alt="" width="10px" height="10px;">百度未收录\');
                }

            },
            error: function(data) {
                        $("#sl").attr("class","badge bg-danger");
                $("#sl").html(\'<img src="../assets/image/bds.png" alt="" width="10px" height="10px;">检测失败\');
            }
        });
                });
</script>
            </body>
            
            </html>';
        }
        $fp=fopen("page/{$data['id']}.html","w");
        if(fwrite($fp,$msg)){
            fclose($fp);
                 $state=1;
        }
        $pdo=null;
        $query=null;
        die(json_encode(["state"=>$state,"name"=>"{$data['id']}.html"]));
        break;
        case 'upload':
            include './system/upload.php';
            $pic_path = "assets/image";
            $protocol =((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
            $url=$protocol.$_SERVER['HTTP_HOST'].substr($PHP_SELF,0,strrpos($PHP_SELF,'/')+1);
            $uppic = $url . '/assets/image/';
            $upload = new upload($pic_path);
            if ($file_name = $upload->up($_FILES['file'])) {
                $status = 1;
                $id = $file_name;
                echo json_encode(['status' => 1, 'url' => $uppic.$id]);
                exit;
            } else {
                $status = 0;
                $msg = $upload->error();
                echo json_encode(['status' => 0, 'msg' => $msg]);
                exit;
            }
            break;
           case 'bdjc':
           $url=daddslashes($_POST["url"]);
               $statue=0;
                        exit(json_encode(["statue"=>$statue]));
            break;
        }
        
        ?>